import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
//  2.创建路由对象
//  懒加载方式 推荐方式
const dashboard = () => import('@/views/dashboard/index')
const login = () => import('@/views/login/index')
const p404 = () => import('@/views/404')
const Clients = () => import('@/views/Clients')
const Weeks = () => import('@/views/Weeks')
const Faults = () => import('@/views/Faults')
const Boards = () => import('@/views/Boards')
const Tips = () => import('@/views/Tips')
const Author = () => import('@/views/Author')
const Headline = () => import('@/views/Headline')
// 用户管理模块
const Roles = () => import('@/views/Roles')
const Users = () => import('@/views/Users')
const Permissions = () => import('@/views/Permissions')
/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: login,
    hidden: true
  },
  {
    path: '/Author',
    component: Author,
    hidden: true
  },
  {
    path: '/404',
    component: p404,
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: dashboard,
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/s0',
    component: Layout,
    redirect: '/s0',
    name: 's0',
    meta: { title: '客户管理', icon: 'example' },
    children: [
      {
        path: 'Clients',
        name: 'Clients',
        component: Clients,
        meta: { title: '客户列表', icon: 'table' }
      }
    ]
  },
  {
    path: '/upadmin',
    component: Layout,
    redirect: '/upadmin',
    name: 'upadmin',
    meta: { title: '上报管理', icon: 'example' },
    children: [
      {
        path: 'Weeks',
        name: 'Weeks',
        component: Weeks,
        meta: { title: '周报管理', icon: 'table' }
      },
      {
        path: 'Faults',
        name: 'Faults',
        component: Faults,
        meta: { title: '故障管理', icon: 'tree' }
      }
    ]
  },
  {
    path: '/sadmin',
    component: Layout,
    redirect: 'sadmin',
    name: 'sadmin',
    meta: { title: '系统管理', icon: 'example' },
    children: [
      {
        path: 'Tips',
        name: 'Tips',
        component: Tips,
        meta: { title: '技巧管理', icon: 'table' }
      },
      {
        path: 'Boards',
        name: 'Boards',
        component: Boards,
        meta: { title: '公告管理', icon: 'tree' }
      },
      {
        path: 'Headline',
        name: 'Headline',
        component: Headline,
        meta: { title: '头条管理', icon: 'tree' }
      }
    ]
  },
  {
    path: '/uadmin',
    component: Layout,
    redirect: '/uadmin',
    name: 'uadmin',
    meta: { title: '用户管理', icon: 'example' },
    children: [
      {
        path: 'Roles',
        name: 'Roles',
        component: Roles,
        meta: { title: '角色管理', icon: 'table' }
      },
      {
        path: 'Users',
        name: 'Users',
        component: Users,
        meta: { title: '用户管理', icon: 'tree' }
      },
      {
        path: 'Permissions',
        name: 'Permissions',
        component: Permissions,
        meta: { title: '权限管理', icon: 'tree' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
